class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def checkEqualTree(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """

        def search(node):
            left_value = 0
            right_value = 0
            if node.left:
                left_value = search(node.left)
            if node.right:
                right_value = search(node.right)
            return node.val + left_value + right_value

        total = search(root)
        if total % 2 == 1:
            return False
        target = total // 2

        def find_split(node):
            left_value = 0
            left_flag = False
            right_value = 0
            right_flag = False
            if node.left:
                left_flag, left_value = search(node.left)
                if left_value == target:
                    left_flag = True
            if node.right:
                right_flag, right_value = search(node.right)
                if right_value == target:
                    right_flag = True
            return left_flag or right_flag, left_value + right_value + node.val

        return find_split(root)[0]
